package com.atguigu.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseDao;
import com.atguigu.base.BaseServiceImpl;
import com.atguigu.dao.CommunityDao;
import com.atguigu.dao.DictDao;
import com.atguigu.entity.Community;
import com.atguigu.service.CommunityService;
import com.atguigu.util.CastUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CommunityService.class)
@Transactional
public class CommunityServiceImpl extends BaseServiceImpl<Community> implements CommunityService {
    @Autowired
    private CommunityDao communityDao;
    @Autowired
    private DictDao dictDao;

    @Override
    protected BaseDao<Community> getEntityDao() {
        return communityDao;
    }

    @Override
    public List<Community> findAll() {
        return communityDao.findAll();
    }

    /*
    */
/**
     * 这个父类的方法重写是因为我们有 特殊要求,需要根据areaId,plateId查询字典表里对应的name并赋值
     * @param filters
     * @return
     *//*

    @Override
    public PageInfo<Community> findPage(Map<String, Object> filters) {
        //启动分页插件
        int pageNum = CastUtil.castInt(filters.get("pageNum"));
        int pageSize = CastUtil.castInt(filters.get("pageSize"));
        PageHelper.startPage(pageNum,pageSize);
        //查询数据库数据获取分页查询的结果
        Page<Community> page = getEntityDao().findPage(filters);
        //拿到获取到的对象数据进行areaId,plateId查询对应的name
        List<Community> communityList= page.getResult();
        for (Community community : communityList) {
            //根据areaId查询并赋值name
            String areaName = dictDao.getById(community.getAreaId()).getName();
            community.setAreaName(areaName);
            //根据plateId查询并赋值name
            String plateName = dictDao.getById(community.getPlateId()).getName();
            community.setPlateName(plateName);
        }
        //Page转换为PageInfo并返回   就是PageInfo通过传入的Page进行更多的包装跟数据处理
        return new PageInfo<>(page.getResult(),10);
    }
*/

    @Override
    public Community getById(Serializable id) {
        Community community = super.getById(id);
        //此处封装小区中的区域名字跟板块  根据ID查询导name
        String areaName = dictDao.getNameById(community.getAreaId());
        community.setAreaName(areaName);
        String plateName = dictDao.getNameById(community.getPlateId());
        community.setPlateName(plateName);
        return community;
    }
}
